package com.pengtu.gsj.service.impl.check;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.check.CheckTableDao;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.utils.MathsUtils;

@Transactional
@Service
public class CheckTableServiceImpl implements CheckTableService {

	@Autowired
	private CheckTableDao checkTableDao;
	
	@Override
	public CheckTable getCheckTable(String id) {
		return checkTableDao.get(id);
	}

	@Override
	public void save(CheckTable checkTable) {
		checkTableDao.save(checkTable);
	}

	@Override
	public void delete(CheckTable checkTable) {
		checkTableDao.delete(checkTable);
	}

	@Override
	public List<CheckTable> getCheckTables(QueryFilter queryFilter) {
		return checkTableDao.find(queryFilter);
	}

	@Override
	public void updateCheckTable(CheckTable checkTable, double percentage,
			double total, double tableFull, List<Object> values) {
		if (StringUtils.isNotEmpty(checkTable.getId())) {
			if (values.size() > 0) {
				if ((checkTable.getPerfection() == null || checkTable.getTableFull() == null) ||
						(checkTable.getPerfection() != percentage || checkTable.getTestFull() != total || checkTable.getTableFull() != tableFull)) {
						//设置系统完善度
						checkTable.setPerfection(percentage);
						//设置检查得分
						checkTable.setTestFull(total);
						//设置检查表总分
						checkTable.setTableFull(tableFull);
						save(checkTable);
					}
			} else {
				//设置系统完善度
				checkTable.setPerfection(0.0);
				//设置检查得分
				checkTable.setTestFull(0.0);
				//设置检查表总分
				checkTable.setTableFull(tableFull);
				save(checkTable);
			}
			
		}
	}
	
	@Override
	public void updateCheckTable(CheckTable checkTable, double percentage, double total, double tableFull, Boolean flag){
		if (StringUtils.isNotEmpty(checkTable.getId())) {
			//0表示没有搜索条件
			if(flag){
				if ((checkTable.getPerfection() == null || checkTable.getTableFull() == null) ||
						(checkTable.getPerfection() != percentage || checkTable.getTestFull() != total || checkTable.getTableFull() != tableFull || checkTable.getTestFull() != null)) {
					//设置系统完善度
					checkTable.setPerfection(percentage);
					//设置检查得分
					checkTable.setTestFull(MathsUtils.formatDouble(total, 2));
					//设置检查表总分
					checkTable.setTableFull(tableFull);
					save(checkTable);
				}
			} 
		}
	}
	
}
